Integration Testing এর মাধ্যমে API Response যাচাই করা

Java Technologies - স্প্রিং বুট ক্লায়েন্ট (Spring Boot Client) - Testing Spring Boot Clients
142

স্প্রিং বুট ক্লায়েন্টে Integration Testing ব্যবহার করে API রেসপন্স যাচাই করার জন্য আপনাকে একটি সঠিক টেস্ট সেটআপ তৈরি করতে হবে। স্প্রিং বুটের জন্য @SpringBootTest, TestRestTemplate, অথবা WebTestClient ব্যবহার করে API-এর ইন্টিগ্রেশন টেস্ট করা যায়।

নিচে উদাহরণসহ Integration Testing-এর প্রক্রিয়া দেখানো হলো:


১. প্রজেক্টে টেস্ট ডিপেনডেন্সি যোগ করা

pom.xml-এ টেস্টিংয়ের জন্য নিচের ডিপেনডেন্সি যোগ করুন:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-webflux</artifactId>
</dependency>

Gradle ব্যবহার করলে:

testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.boot:spring-boot-starter-webflux'

২. TestRestTemplate ব্যবহার করে Integration Test

ক্লাস উদাহরণ

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.ResponseEntity;

import static org.assertj.core.api.Assertions.assertThat;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ApiIntegrationTest {

    @Autowired
    private TestRestTemplate restTemplate;

    @Test
    public void testGetApiResponse() {
        String url = "/api/resource"; // আপনার এন্ডপয়েন্ট
        ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);

        assertThat(response.getStatusCodeValue()).isEqualTo(200); // HTTP Status চেক
        assertThat(response.getBody()).contains("expected content"); // কনটেন্ট যাচাই
    }
}

TestRestTemplate বৈশিষ্ট্য

  • সহজেই HTTP কল করতে পারে।
  • API-এর জন্য সঠিক HTTP স্ট্যাটাস এবং রেসপন্স যাচাই করা যায়।
  • Spring Boot Context-এর মধ্যে কাজ করে।

৩. WebTestClient ব্যবহার করে Integration Test (Reactive API এর জন্য)

ক্লাস উদাহরণ

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.reactive.server.WebTestClient;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ReactiveApiIntegrationTest {

    private final WebTestClient webTestClient;

    public ReactiveApiIntegrationTest(WebTestClient.Builder webTestClientBuilder) {
        this.webTestClient = webTestClientBuilder
                .baseUrl("http://localhost:8080") // আপনার অ্যাপের বেস URL
                .build();
    }

    @Test
    public void testGetApiResponse() {
        webTestClient.get()
                .uri("/api/resource")
                .exchange()
                .expectStatus().isOk() // HTTP Status যাচাই
                .expectBody()
                .jsonPath("$.key").isEqualTo("expectedValue"); // JSON ফিল্ড যাচাই
    }
}

WebTestClient বৈশিষ্ট্য

  • Reactive API টেস্টের জন্য উপযোগী।
  • JSON রেসপন্স বা নির্দিষ্ট ফিল্ড চেক করার জন্য সহজ JSONPath সাপোর্ট।

৪. Mocking External APIs Using WireMock

WireMock ব্যবহার করে বাইরের API গুলোর জন্য সিমুলেটেড রেসপন্স তৈরি করা যায়।

WireMock Dependency

<dependency>
    <groupId>com.github.tomakehurst</groupId>
    <artifactId>wiremock</artifactId>
    <scope>test</scope>
</dependency>

WireMock Integration Example

import com.github.tomakehurst.wiremock.client.WireMock;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.web.reactive.function.client.WebClient;

import static com.github.tomakehurst.wiremock.client.WireMock.*;

@SpringBootTest
public class WireMockIntegrationTest {

    private WireMockServer wireMockServer;

    @Autowired
    private WebClient.Builder webClientBuilder;

    @BeforeEach
    public void setup() {
        wireMockServer = new WireMockServer(8089); // WireMock Server এর পোর্ট
        wireMockServer.start();
        WireMock.configureFor("localhost", 8089);
    }

    @AfterEach
    public void teardown() {
        wireMockServer.stop();
    }

    @Test
    public void testExternalApiCall() {
        // Mock API Response
        stubFor(get(urlEqualTo("/external-api"))
                .willReturn(aResponse()
                        .withStatus(200)
                        .withBody("{\"message\": \"success\"}")));

        // Call API
        WebClient webClient = webClientBuilder.baseUrl("http://localhost:8089").build();
        String response = webClient.get()
                .uri("/external-api")
                .retrieve()
                .bodyToMono(String.class)
                .block();

        // Assertion
        assertThat(response).contains("success");
    }
}

৫. টেস্ট রেসাল্ট যাচাই

টেস্ট চালানোর সময়:

  • @SpringBootTest অ্যাপ্লিকেশনের পুরো প্রসঙ্গ লোড করে।
  • Mocked APIs প্রকৃত API-এর বিকল্প হিসেবে কাজ করে।
  • JSONPath বা স্ট্যাটাস চেক ব্যবহার করে রেসপন্স যাচাই করা হয়।

Run Test

mvn test
# বা
gradle test

সারাংশ

  1. TestRestTemplate: সিম্পল HTTP API ইন্টিগ্রেশন টেস্টের জন্য।
  2. WebTestClient: Reactive API টেস্টের জন্য।
  3. WireMock: বাইরের API-র জন্য Mock করা রেসপন্স।

উপরের উদাহরণগুলোর মাধ্যমে স্প্রিং বুট ক্লায়েন্টের জন্য সহজেই Integration Testing সেটআপ এবং API রেসপন্স যাচাই করতে পারবেন।

Content added By
Promotion
NEW SATT AI এখন আপনাকে সাহায্য করতে পারে।

Are you sure to start over?

Loading...